package ExecuteEngine;

import RecordManagement.*;
import SystemManagement.*;
import parse.*;

public class ExecuteEngine {
	public Operation PQplan;
	public RM_Manager rm;
	public SM_Manager sm;
	
	public ExecuteEngine(RM_Manager r, SM_Manager s){
		this.rm = r;
		this.sm = s;
	}
	
	public Operation PQPlan(LQPlan plan){
		this.PQplan = LogToPhy(plan.GetRoot());
		return PQplan;
	}
	
	public Operation LogToPhy(Algebra plan){
		if(plan instanceof AlgebraTable)
			return new TableScan(((AlgebraTable)plan).tableName, rm, sm);
		else
		if(plan instanceof Descartes){
			Operation Op1 = LogToPhy(((Descartes)plan).GetA1()),
			          Op2 = LogToPhy(((Descartes)plan).GetA2());
			
			return new CrossProduct(Op1, Op2);
		}
		else
		if(plan instanceof Pi)
			return new Project(((Pi)plan).attr_list, LogToPhy(((Pi)plan).son));
		else
		if(plan instanceof Sigma)
			return new Select(((Sigma)plan).conditions, LogToPhy(((Sigma)plan).son));
		
		return null;
	}
}
